package server;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.SQLException;
import java.util.concurrent.Semaphore;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class ServerDatabase {
	private static ServerSocket server;
	private int porta=47624;
	private static Database d;
	private static int pid=0;
	private static JFrame console;
	private static JTextArea area;
	private static Semaphore mutex=new Semaphore(1);
	private static JScrollPane scroll;
	private static Image immagineIc;
	private static com.apple.eawt.Application application;
	private static int numConnessioni=-1;
	
	public static void main(String[] args) throws SQLException, IOException {
		ServerDatabase sd=new ServerDatabase();
	}
	
	public ServerDatabase() throws SQLException, IOException{
		immagineIc = Toolkit.getDefaultToolkit().getImage(
				"Risorse/servericon.png");
//		MAC
		if (System.getProperty("os.name").equals("Mac OS X")) {
			  application = com.apple.eawt.Application.getApplication();
			  application.setDockIconImage(immagineIc);
		}
//		MAC
		server=new ServerSocket(porta);
		console=new JFrame();
		com.apple.eawt.FullScreenUtilities.setWindowCanFullScreen(console,true);
		console.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		console.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e){
				int option = JOptionPane
						.showConfirmDialog(
								null,
								"Continuare ? Uscendo si interrompera' il SERVER!",
								"Attenzione", JOptionPane.YES_NO_OPTION);
				if(option==JOptionPane.YES_OPTION){
					try {
						server.close();
						System.exit(0);
					} catch (IOException e1) {}
				}
			}
		});
		console.setSize(800,500);
		console.setLocation(300,100);
		area=new JTextArea();
		scroll=new JScrollPane(area);
		area.setEditable(false);
		area.setForeground(Color.RED);
		console.add(scroll);
		console.setVisible(true);
		area.append("Server Attivo\n");
		area.append(server.getLocalSocketAddress().toString()+"\n");
		d=new Database();
		incrementaIcon();
		while(true){
			accettaRicheste();
		}
	}
	public void incrementaIcon() {
		numConnessioni++;
		BufferedImage originalIcon = (BufferedImage) application.getDockIconImage();
	    BufferedImage newIcon = new BufferedImage(originalIcon.getWidth(), originalIcon.getHeight(), BufferedImage.TYPE_INT_ARGB);

	    Graphics2D graphics = (Graphics2D) newIcon.getGraphics();

	    graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
	    graphics.setColor(Color.decode("#E40000"));

	    graphics.drawImage(originalIcon, 0, 0, null);

	    graphics.fillOval(originalIcon.getWidth()-110,0, 100, 100);

	    graphics.setColor(Color.WHITE);
	    int gran=100;
	    if(numConnessioni>10) gran=60;
	    graphics.setFont(new Font("Helvetica", Font.BOLD, gran));
	    graphics.drawString(Integer.toString(numConnessioni), originalIcon.getWidth()-90, 90);

	    graphics.dispose();

	    application.setDockIconImage(newIcon);
	}
	
	public void diminuisciIcon() {
		numConnessioni--;
		BufferedImage originalIcon = (BufferedImage) application.getDockIconImage();
	    BufferedImage newIcon = new BufferedImage(originalIcon.getWidth(), originalIcon.getHeight(), BufferedImage.TYPE_INT_ARGB);

	    Graphics2D graphics = (Graphics2D) newIcon.getGraphics();

	    graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
	    graphics.setColor(Color.decode("#E40000"));

	    graphics.drawImage(originalIcon, 0, 0, null);

	    graphics.fillOval(originalIcon.getWidth()-110,0, 100, 100);

	    graphics.setColor(Color.WHITE);
	    int gran=100;
	    if(numConnessioni>10) gran=60;
	    graphics.setFont(new Font("Helvetica", Font.BOLD, gran));
	    graphics.drawString(Integer.toString(numConnessioni), originalIcon.getWidth()-90, 90);

	    graphics.dispose();

	    application.setDockIconImage(newIcon);
	}
	

	private void accettaRicheste() throws IOException {
		Socket client=server.accept();
		incrementaIcon();
		pid++;
		GestoreClient c=new GestoreClient(client,d,mutex,area,pid);
		c.setServerDatabase(this);
		c.start();
	}
}
